﻿// ===============================================================================
// Project Name        :	Weisenz.Core.Control
// Project Description :	Weisenz系统核心类库。
// ===============================================================================
// Class Name          :	DialogBox
// Class Version       :	v1.0.0.0
// Class Description   :	系统对话框控件类。
// Author              :	Charles
// Create Time         :	2012/3/29 
// Update Time         :	2012/3/29 
// ===============================================================================
// Copyright © Weisenz 2012 . All rights reserved.
// ===============================================================================
using System.Text;
using System.Web.UI;

namespace Weisenz.Core.Control
{
    /// <summary>
    /// 系统对话框控件类。
    /// </summary>
    public class DialogBox
    {
        /// <summary>
        /// 警告对话框
        /// </summary>
        /// <param name="alertMessage">警告信息</param>
        /// <param name="page">当前页面指针，一般为this</param>
        public static void Alert(string alertMessage, Page page)
        {
            string script = "<script language=javascript>window.alert('" + alertMessage + "')</script>";
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "alert"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "alert", script);
            }
        }

        /// <summary>
        /// 确认对话框
        /// </summary>
        /// <param name="confirmMessage">确认信息</param>
        /// <param name="page">当前页面指针，一般为this</param>
        public static void Confirm(string confirmMessage, Page page)
        {
            string content = "<script language=javascript>window.confirm('" + confirmMessage + "')</script>";
            if (!page.ClientScript.IsClientScriptBlockRegistered(page.GetType(), "confirmwindow"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "confirmwindow", content);
            }
        }

        /// <summary>
        /// 提示输入对话框
        /// </summary>
        /// <param name="promptMessage">提示信息</param>
        /// <param name="defaultValue">输入框的默认值</param>
        /// <param name="page">当前页面指针，一般为this</param>
        public static void Prompt(string promptMessage, string defaultValue, Page page)
        {
            string content = "<script language=javascript>window.prompt('" + promptMessage + "','" + defaultValue + "')</script>";
            if (!page.ClientScript.IsClientScriptBlockRegistered(page.GetType(), "promptwindow"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "promptwindow", content);
            }
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        public static void CloseWindow(Page page)
        {
            string content = "<script language=javascript>window.close();</script>";
            if (!page.ClientScript.IsClientScriptBlockRegistered(page.GetType(), "closewindow"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "closewindow", content);
            }
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("top.location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());
        }
    }
}
